package 力扣热题100;

/**
 * 搜索旋转排列数组
 */
public class LeetCode17 {
    public int search(int[] nums, int target) {
        if(nums.length==1){
            return target==nums[0]?0:-1;
        }
        int l = 0;
        int r = nums.length - 1;
        while (l <= r) {
            int tmp = (l + r) / 2;
            if (target == nums[tmp]) {
                return tmp;
            }
            if (nums[l] <= nums[tmp]) {//左边有序
                if (target >= nums[l] && target < nums[tmp]) {//在左边
                    r = tmp - 1;
                } else {//不在左边
                    l = tmp+1;
                }
            } else {//右边有序
                if (target > nums[tmp] && target <= nums[r]) {//在右边
                    l = tmp + 1;
                } else {//不在右边
                    r = tmp - 1;
                }
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        LeetCode17 leetCode17=new LeetCode17();
        int[]arr=new int[]{3,1};
        leetCode17.search(arr,1);
    }


}
